<template>
	<view class="p-10">
		<view class="card">
			<my-forms ref="valiForm" :model="valiFormData" :label-position="'top'" :formItem="formData"></my-forms>
			<u-button type="primary" shape="circle" text="提交修改" @click="submit"></u-button>
		</view>
	</view>
</template>
<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { computed, ref } from 'vue';
import { listRiskPointManagement, getPostRLHLRel } from '@/api/riskcontrol.js';
import { infoHiddenDanger, editHiddenDanger } from '@/api/hidden-danger.js';

// 校验表单数据
let formData = computed(() => {
	return [
		{
			itemType: 'date',
			type: 'date',
			label: '检查日期',
			required: true,
			prop: 'checkDate'
		},
		{
			itemType: 'select',
			type: 'hidden_insert_type',
			label: '检查类型',
			required: true,
			prop: 'dataType'
		},
		{
			// itemType: 'select',
			// type: 'GETuseList',
			label: '检查人',
			required: true,
			prop: 'inspectUserNames'
			// multiple: true
		},
		{
			itemType: 'select',
			label: '检查地点',
			list: checkList.value,
			required: true,
			prop: 'riskLocationId'
		},
		{
			label: '隐患内容',
			type: 'textarea',
			required: true,
			prop: 'hiddenDangerInfo'
		},
		{
			itemType: 'select',
			label: '隐患成因',
			type: 'HIDDEN_DANGER_FACTOR',
			prop: 'hiddenDangerFactor'
		},
		{
			itemType: 'select',
			label: '隐患等级',
			required: true,
			list: hiddenList.value,
			prop: 'hiddenLevel'
		},
		{
			itemType: 'select',
			type: 'GEThiddenDangerType',
			label: '隐患类型',
			required: true,
			prop: 'hiddenType'
		},
		{
			itemType: 'cascader',
			type: 'GETdeptTree',
			label: '责任单位',
			required: true,
			prop: 'dutyDeptId'
		},
		{
			itemType: 'select',
			type: 'GETuseList',
			label: '责任人',
			required: true,
			prop: 'dutyUserId'
		},
		{
			itemType: 'radio',
			list: [
				{
					label: '限期整改',
					value: 0
				},
				{
					label: '现场整改',
					value: 1
				}
			],
			label: '整改方式',
			required: true,
			prop: 'changeType'
		},
		{
			itemType: 'date',
			type: 'date',
			label: '限期日期',
			required: true,
			prop: 'requireChangeEndDate',
			show: valiFormData.value.changeType == 0
		},
		{
			itemType: 'select',
			type: 'GETuseList',
			label: '整改人',
			required: true,
			show: valiFormData.value.changeType == 1,
			prop: 'trueChangeUserId'
		},
		{
			label: '整改措施',
			type: 'textarea',
			required: true,
			show: valiFormData.value.changeType == 1,
			prop: 'changeInfo'
		},
		{
			itemType: 'select',
			type: 'GETuseList',
			multiple: true,
			label: '验收人',
			required: true,
			prop: 'checkUserIds',
			show: valiFormData.value.changeType == 1
		},
		{
			itemType: 'image',
			label: '现场图片',
			prop: 'filePaths'
		}
	].filter((i) => i);
});
const valiForm = ref(null);
const valiFormData = ref({});
// 保存提交
const submit = () => {
	valiForm.value.$refs.refMyForm
		.validate()
		.then(async (res) => {
			valiFormData.value.hiddenChangeRequire.changeType = valiFormData.value.changeType;
			valiFormData.value.hiddenChangeRequire.requireChangeEndDate = valiFormData.value.requireChangeEndDate;
			valiFormData.value.hiddenChangeRequire.trueChangeUserId = valiFormData.value.trueChangeUserId;
			valiFormData.value.hiddenChangeRequire.changeInfo = valiFormData.value.changeInfo;
			valiFormData.value.hiddenChangeRequire.changeMoney = valiFormData.value.changeMoney;
			const { code, msg } = await editHiddenDanger(valiFormData.value);
			if (code == 200) {
				uni.showToast({
					icon: 'success',
					title: `修改成功`
				});
				// valiFormData.value.inspectUsers = []
				uni.navigateBack({
					delta: 2 // 返回的页面数，如果是1则返回上一页
				});
			} else {
				uni.showToast({
					title: msg
				});
			}
		})
		.catch((err) => {});
};
// 获取检查地点列表
let checkList = ref([]);
const getCheckPonit = async () => {
	const { data } = await listRiskPointManagement();
	checkList.value = data.map((item) => ({
		dictLabel: item.riskLocationName,
		dictValue: item.id
	}));
};
getCheckPonit();
// 获取岗位对应隐患风险级别数组
let hiddenList = ref([]);
const gethiddenList = async () => {
	const { data } = await getPostRLHLRel();
	hiddenList.value = data?.hdLevels;
};
gethiddenList();
onLoad(async (e) => {
	const { data } = await infoHiddenDanger({
		id: e.id
	});
	valiFormData.value = data;
	valiFormData.value.dataType = data.dataType + '';
});
</script>
<style lang="scss" scoped></style>